﻿//Copyright (c) Microsoft Corporation. All rights reserved.

using Microsoft.Ted.Wacel.TestConsole.Properties;
using Microsoft.Ted.Wacel.TableStorage;
using System;
using System.Linq;
using System.Diagnostics;
using System.Threading;
using System.Collections.Generic;

namespace Microsoft.Ted.Wacel.TestConsole
{
    public class CloudTableStorageTester
    {
        public static void  BasicCrud()
        {
            CloudTableStorage<Customer> storage = new CloudTableStorage<Customer>("ID", Settings.Default.StorageConnectionString, "customers", false);

            storage.Clear();

            DateTime time = DateTime.UtcNow;

            storage.Insert(new Customer { ID = 100, Name = "This is a customer.", Value = 123.456, Time = time});
            
            var customer = storage.Read("100");

            VisualTester.IsNotNull(customer);
            VisualTester.AreEqual(100, customer.ID);
            VisualTester.AreEqual("This is a customer.", customer.Name);
            VisualTester.AreEqual(123.456, customer.Value);
            VisualTester.AreEqual(0, (customer.Time-time).TotalSeconds);

            storage.Delete("100");
            customer = storage.Read("100");
            VisualTester.IsNull(customer);
        }
        public static void BasicPage()
        {
            CloudTableStorage<Customer> storage = new CloudTableStorage<Customer>("ID", Settings.Default.StorageConnectionString, "customers", false);

            storage.Clear();

            DateTime time = DateTime.UtcNow;

            for (int i = 0; i < 100; i++ )
                storage.Insert(new Customer { ID = i, Name = "This is a customer " + i.ToString(), Value = i + 0.5, Time = time });

            var customers = storage.List("50", "59");
            VisualTester.AreEqual(10, customers.Count());
            VisualTester.AreEqual("This is a customer 50", customers.First().Name);
        }

        public static void PageWithPartition()
        {
            CloudTableStorage<Customer> storage = new CloudTableStorage<Customer>("Company", "ID", Settings.Default.StorageConnectionString, "customers", false);

            storage.Clear();

            DateTime time = DateTime.UtcNow;

            for (int i = 0; i < 100; i++)
                storage.Insert(new Customer { ID = i, Name = "This is a customer " + i.ToString(), Value = i + 0.5, Time = time, Company =
                       (i % 2 == 0 ? "Company0": "Company1") });

            var customers = storage.List("50", "59", "Company0", "Company0");
            VisualTester.AreEqual(5, customers.Count());
            VisualTester.AreEqual("This is a customer 58", customers.Last().Name);
        }
        public static void PageWithPartition2()
        {
            CloudTableStorage<Customer> storage = new CloudTableStorage<Customer>("Company", "ID", Settings.Default.StorageConnectionString, "customers", false);

            storage.Clear();

            DateTime time = DateTime.UtcNow;

            for (int i = 0; i < 100; i++)
                storage.Insert(new Customer
                {
                    ID = i,
                    Name = "This is a customer " + i.ToString(),
                    Value = i + 0.5,
                    Time = time,
                    Company =
                        (i % 2 == 0 ? "Company0" : "Company1")
                });

            var customers = storage.List("50", "59", "Company0", "Company1");
            VisualTester.AreEqual(10, customers.Count());
            VisualTester.AreEqual("This is a customer 59", customers.Last().Name);
        }

        public static void LamdaQuery()
        {
            CloudTableStorage<Customer> storage = new CloudTableStorage<Customer>("ID", Settings.Default.StorageConnectionString, "customers", false);
            storage.Clear();
            for (int i = 0; i < 30; i++)
            {
                DateTime time = DateTime.UtcNow;
                storage.Insert(new Customer { ID = i, Name = "This is a customer " + i.ToString(), Value = i * 1.0, Time = time });
            }

            List<Customer> customers = storage.List(c => c.ID>=15).ToList();
            VisualTester.AreEqual(15, customers.Count);
        }
    }
   
}
